import {
    editAddress,
    getAddressDetail,
    substationaddr,
    getabode,
    getregioncity
} from '../../api/user.js';
import {
    setFormId
} from '../../api/api.js';

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '添加地址'
        },
        cartId: '', //购物车id
        pinkId: 0, //拼团id
        couponId: 0, //优惠券id
        id: 0, //地址id
        userAddress: {
            is_default: false
        }, //地址详情
        abode: '请选择所在小区',
        abodeindex: 0,
        address: '请选择地区',
        area: '',
        customIndex: [0, 0, 0],
        onlyArray: [
            [],
            [],
            []
        ],
        customArray: [],
        pid: '',
        detail_hidden: false,
        detailed_hidden: true
    },
    /**
     * 授权回调
     * 
     */
    onLoadFun: function() {
        this.getUserAddress();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            cartId: options.cartId || '',
            pinkId: options.pinkId || 0,
            couponId: options.couponId || 0,
            id: options.id || 0,
            'parameter.title': options.id ? '修改地址' : '添加地址'
        });


        substationaddr().then(res => {
            var data = {
                customArray: res.data,
                customIndex: this.data.customIndex,
                onlyArray: this.data.onlyArray,
            };

            for (var i = 0; i < data.customArray.length; i++) {
                data.onlyArray[0].push(data.customArray[i].name);
            }
            for (var j = 0; j < data.customArray[data.customIndex[0]].child.length; j++) {
                data.onlyArray[1].push(data.customArray[data.customIndex[0]].child[j].name);
            }
            for (var k = 0; k < data.customArray[data.customIndex[0]].child[data.customIndex[1]].child.length; k++) {
                data.onlyArray[2].push(data.customArray[data.customIndex[0]].child[data.customIndex[1]].child[k].name);
            }
            this.setData(data);

        });

        getregioncity().then(res => {
            console.log(res.data)
            this.setData({
                site:res.data
            })
        });
    },

    //多列自定义选择器改变value的方法
    bindCustomPickerChange: function(e) {
        var customArray = this.data.customArray,
            customIndex = this.data.customIndex,
            onlyArray = this.data.onlyArray;
        var province = onlyArray[0][customIndex[0]]
        var city = onlyArray[1][customIndex[1]]

        if (this.getIndex(this.data.site, city)) {
            this.setData({
                detail_hidden: true,
                detailed_hidden: false,
                detailed_addr: ''
            })
        } else {
            this.setData({
                detail_hidden: false,
                detailed_hidden: true,
                detail: ''
            })
        }
        var district = onlyArray[2][customIndex[2]]
        var address = [province, city, district]
        this.setData({
            customIndex: e.detail.value,
            address: address,
            province: province,
            city: city,
            district: district,
            pid: customArray[customIndex[0]].child[customIndex[1]].child[customIndex[2]].id
        })

        this.getabode()
    },

    // 多列自创选择器换列方法
    bindCustomPickerColumnChange: function(e) {
        var customArray = this.data.customArray,
            customIndex = this.data.customIndex,
            onlyArray = this.data.onlyArray;

        customIndex[e.detail.column] = e.detail.value;

        var searchColumn = () => {
            for (var i = 0; i < customArray.length; i++) {
                var arr1 = [];
                var arr2 = [];
                if (i == customIndex[0]) {
                    for (var j = 0; j < customArray[i].child.length; j++) {
                        arr1.push(customArray[i].child[j].name);
                        if (j == customIndex[1]) {
                            for (var k = 0; k < customArray[i].child[j].child.length; k++) {
                                arr2.push(customArray[i].child[j].child[k].name);
                            }
                            onlyArray[2] = arr2;
                        }
                    }
                    onlyArray[1] = arr1;
                }
            };
        }

        switch (e.detail.column) {
            case 0:
                customIndex[1] = 0;
                customIndex[2] = 0;
                searchColumn();
                break;
            case 1:
                customIndex[2] = 0;
                searchColumn();
                break;
        }
        this.setData({
            onlyArray: onlyArray,
            customIndex: customIndex
        });
    },

    getindex2: function(key) {
        var customArray = this.data.customArray,
            customIndex = this.data.customIndex,
            onlyArray = this.data.onlyArray;
        var searchColumn = () => {
            for (var i = 0; i < customArray.length; i++) {
                var arr1 = [];
                var arr2 = [];
                if (i == customIndex[0]) {
                    for (var j = 0; j < customArray[i].child.length; j++) {
                        arr1.push(customArray[i].child[j].name);
                        if (j == customIndex[1]) {
                            for (var k = 0; k < customArray[i].child[j].child.length; k++) {
                                arr2.push(customArray[i].child[j].child[k].name);
                            }
                            onlyArray[2] = arr2;
                        }
                    }
                    onlyArray[1] = arr1;
                }
            };
        }

        switch (key) {
            case 0:
                customIndex[1] = 0;
                customIndex[2] = 0;
                searchColumn();
                break;
            case 1:
                customIndex[2] = 0;
                searchColumn();
                break;
        }
        this.setData({
            onlyArray: onlyArray,
            customIndex: customIndex
        });
    },

    // 选择所在小区
    getabode: function(a = '') {
        getabode({
            pid: this.data.pid
        }).then(res => {
            this.setData({
                area: res.data,
                abodeindex: res.data.indexOf(this.data.abode)
            })
            if (a == '') {
                this.setData({
                    abode: '请选择所在小区',
                    abodeindex: 0
                })
            }
        });
    },

    getUserAddress: function() {
        if (!this.data.id) return false;
        var that = this;
        getAddressDetail(this.data.id).then(res => {
            console.log(res)
            var address = [res.data.province, res.data.city, res.data.district];
            var customArray = this.data.customArray,
                customIndex = this.data.customIndex;

            customIndex[0] = this.getIndex(this.data.onlyArray[0], res.data.province);
            this.getindex2(0);
            customIndex[1] = this.getIndex(this.data.onlyArray[1], res.data.city);
            this.getindex2(1);
            customIndex[2] = this.getIndex(this.data.onlyArray[2], res.data.district);
            this.getindex2(2);

            var pid = customArray[customIndex[0]].child[customIndex[1]].child[customIndex[2]].id;

            if (this.getIndex(this.data.site, res.data.city)) {
                this.setData({
                    detail_hidden: true,
                    detailed_hidden: false
                })
            } else {
                this.setData({
                    detail_hidden: false,
                    detailed_hidden: true
                })
            }

            that.setData({
                userAddress: res.data,
                address: address,
                abode: res.data.abode,
                pid: pid,
                detailed_addr: res.data.detail,
                detail: res.data.detail
            });
            that.getabode(1);
        });
    },
    getIndex: function(arr, item) {
        for (var i in arr) {
            if (arr[i] == item) {
                return i;
            };
        };
        return false;
    },
    /**
     * 提交用户添加地址
     * 
     */
    formSubmit: function(e) {
        var that = this,
            value = e.detail.value,
            formId = e.detail.formId;

        console.log(that.data.province)
        console.log(that.data.userAddress)

        if (!value.real_name) return app.Tips({
            title: '请填写收货人姓名'
        });
        if (!value.phone) return app.Tips({
            title: '请填写联系电话'
        });
        if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return app.Tips({
            title: '请输入正确的手机号码'
        });
        if (that.data.province == '请选择所在地区' || that.data.userAddress.province == '') return app.Tips({
            title: '请选择所在地区'
        });

        if (that.getIndex(that.data.site, that.data.city)) {
            value.detail = value.detailed_addr
            that.data.abode = ' '
            that.setData({
                abode: that.data.abode
            })
            if (!value.detailed_addr) return app.Tips({
                title: '请填写详细地址'
            })
        }

        if (that.data.abode == '请选择所在小区') return app.Tips({
            title: '请选择所在小区'
        });
        if (!value.detail) return app.Tips({
            title: '请填写门牌号'
        });

        value.id = that.data.id;
        value.abode = that.data.abode
        value.address = {
            province: that.data.address[0],
            city: that.data.address[1],
            district: that.data.address[2],
        };
        value.is_default = that.data.userAddress.is_default ? 1 : 0;
        setFormId(formId)
        editAddress(value).then(res => {
            if (that.data.id)
                app.Tips({
                    title: '修改成功',
                    icon: 'success'
                });
            else
                app.Tips({
                    title: '添加成功',
                    icon: 'success'
                });
            setTimeout(function() {
                if (that.data.cartId) {
                    var cartId = that.data.cartId;
                    var pinkId = that.data.pinkId;
                    var couponId = that.data.couponId;
                    that.setData({
                        cartId: '',
                        pinkId: '',
                        couponId: ''
                    })
                    wx.navigateTo({
                        url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
                    });
                } else {
                    wx.navigateBack({
                        delta: 1
                    });
                }
            }, 1000);
        }).catch(err => {
            return app.Tips({
                title: err
            });
        })
    },
    ChangeIsDefault: function(e) {
        this.setData({
            'userAddress.is_default': !this.data.userAddress.is_default
        });
    },
    bindPickerChange_hx: function(e) {
        var index = e.detail.value
        this.setData({
            abodeindex: index,
            abode: this.data.area[index]
        })
    },
})